package com.wencai.util;

import com.wencai.bean.IndexStocks;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author wangpan
 * @date 2023/5/10
 */
public class CombineUtils {
    private static Pattern pattern = Pattern.compile("([^\\(\\)]+)\\(([.0-9]+)\\):(.*)");
    private static Pattern stockPattern = Pattern.compile("\\s*([a-zA-Z\u4e00-\u9fa5]+_[\\.0-9a-zA-Z]+)\\s*([.0-9]++)\\s*");

    public static List<IndexStocks> convertData(List<String> dataList, String date) {
        List<IndexStocks> list = new ArrayList<>();
        int i = 0;
        for (String str : dataList) {
            IndexStocks indexStocks = new IndexStocks();
            Matcher matcher = pattern.matcher(str);
            matcher.matches();
            String indexName = matcher.group(1);
            String indexIncrement = matcher.group(2);
            indexStocks.setIndexName(indexName);
            indexStocks.setIndexIncrement(indexIncrement);
            String stocks = matcher.group(3).trim();
            indexStocks.setDate(date);
            indexStocks.setOrder(String.valueOf(++i));
            if (!stocks.matches("\\s+")) {
                Matcher stockMatcher = stockPattern.matcher(stocks);
                if (stockMatcher.find()) {
                    String[] s = stockMatcher.group(1).split("_");
                    indexStocks.setFirstStockName(s[0]);
                    indexStocks.setFirstStockCode(s[1]);
                    indexStocks.setFirstStockIncrement(stockMatcher.group(2));
                }
                if (stockMatcher.find()) {
                    String[] s = stockMatcher.group(1).split("_");
                    indexStocks.setSecondStockName(s[0]);
                    indexStocks.setSecondStockCode(s[1]);
                    indexStocks.setSecondStockIncrement(stockMatcher.group(2));
                }
                if (stockMatcher.find()) {
                    String[] s = stockMatcher.group(1).split("_");
                    indexStocks.setThirdStockName(s[0]);
                    indexStocks.setThirdStockCode(s[1]);
                    indexStocks.setThirdStockIncrement(stockMatcher.group(2));
                }
            }
            list.add(indexStocks);
        }
        return list;
    }
}
